package com.leetcode.Offer;

public class JZ15_2 {
    public static int hammingWeight(int n) {
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        }

        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            if (i % 2 == 1) {
                dp[i] = dp[i - 1] + 1;
            } else {
                dp[i] = dp[i / 2];
            }
        }
        return dp[n];
    }

    public static int hammingWeight_2(int n) {
        int count = 0;
        while (n!=0){
            count++;
            n = (n-1)&n;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(hammingWeight(3));
        System.out.println(hammingWeight_2(3));
    }
}
